﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Xamarin.Essentials;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;

namespace ZXXFAPP.Views
{
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class SplashPage : ContentPage
    {
        public string serverAddress { get; set; }
        // 在类的定义中添加一个Timer对象，其回调函数是OnTimer方法
        System.Timers.Timer timer;
        public SplashPage()
        {
            InitializeComponent();
            //从preference 中获取默认的浏览地址
            serverAddress = Preferences.Get("SERVER", "168.168.100.125:10000");
            timer = new System.Timers.Timer(5000); // 设置间隔为3秒
            timer.Elapsed += OnTimer; // 设置回调函数为OnTimer
            //timer.AutoReset = true; // 设置自动重置为true，使得定时器可以多次触发
            timer.Enabled = true; // 启动定时器
        }


        private async void OnSettingClicked(object sender, EventArgs e)
        {
            timer.Stop();

            //Device.BeginInvokeOnMainThread(async () =>
            //{
            //    //弹出扫描页面
            //    await Shell.Current.Navigation.PushModalAsync(new ScanPage());
            //});

            //return;

            AddressText.Text = serverAddress;
            //显示隐藏的三个控件，隐藏原来的登录倒数
            BtnSetting.IsVisible = false; 
            SettingGroup.IsVisible = true;
        }
        //添加一个定时器计时3秒后跳转到主页
        private void OnTimer(object sender, EventArgs e)
        {
            Device.BeginInvokeOnMainThread(async () =>
            {
                //await Shell.Current.GoToAsync("//LogonPage");
                await Shell.Current.GoToAsync("//WebViewPage");
            });
        }
        private void OnLogonClicked(object sender, EventArgs e)
        {
            //BtnSetting.IsVisible = true;
            //SettingGroup.IsVisible = false;
            //return;
            //保存设置的服务器地址
            serverAddress = AddressText.Text;
            Preferences.Set("SERVER", serverAddress);
            Device.BeginInvokeOnMainThread(async () =>
            {
                //await Shell.Current.GoToAsync("//LogonPage");
                await Shell.Current.GoToAsync("//WebViewPage");
            });
        }
    }
}